package com.lagou.niancheng.lagou.service.common;

import java.text.MessageFormat;

/**
 * com.lagou.niancheng.lagouserviceuser.common.ResultBuilder
 *
 * @author 大犬
 * @date 2020/6/28
 */
public class ResultBuilder {
    private ResultBuilder() {

    }

    public static <T> Result<T> toSuccess(T data) {
        Result<T> result = new Result<>();
        result.setData(data);
        result.setSuccess(true);
        return result;
    }

    public static Result toError(String errorCode, String errorMsg, Object... params) {
        Result result = new Result<>();
        result.setSuccess(false);
        result.setErrorCode(errorCode);
        if (params == null || params.length == 0) {
            result.setErrorMsg(errorMsg);
        } else {
            result.setErrorMsg(MessageFormat.format(errorCode, params));
        }
        return result;
    }

    public static Result toError(ErrorCode errorCode, Object... params) {
        return toError(errorCode.getErrorCode(), errorCode.getErrorMsg(), params);
    }
}
